package com.csit.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.csit.bean.Pager;
import com.csit.dao.ProductDao;
import com.csit.entity.Product;
import com.csit.service.ProductService;
import com.csit.util.ListAndJsonUtil;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product, Integer> implements ProductService {
	
	@Resource
	ProductDao productDao;
	
	@Resource
	public void setBaseDao(ProductDao productDao) {
		super.setBaseDao(productDao);
	}

	public String queryProduct(Pager pager, Product product) {
		if(pager==null){
			pager = new Pager();
		}
		if(product==null){
			product = new Product();
		}
		pager = productDao.queryProduct(pager, product);
		String[] properties = {"productID","productCode","productName","currentQuantity","productSize","productPrice","productItemID","productItemName","unitID","unitName","note"};
		String jsonArray = ListAndJsonUtil.getJSONString(pager.getList(), properties, pager.getTotalCount());
		return jsonArray;
	}

	public boolean isUnique(Product product) {
		
		String[] propertiesName = {"productCode","shop"};
		List<Product> list = getList(propertiesName, product);
		//新建
		if(product.getProductID()==null){
			if(list!=null&&list.size()>0) return false;
		//修改
		}else{
			if(list==null||list.size()==0){
				return true;
			}else if(list.size()==1){
				Product old = list.get(0);
				if(product.getProductID()==old.getProductID()){
					return true;
				}
				return false;
			}else if(list.size()>=2){
				return false;
			}
		}
		return true;
	}

}
